package com.etms.service.impl;

import com.etms.mapper.UserMapper;
import com.etms.pojo.SysUser;
import com.etms.service.UserService;
import com.etms.vo.PageParmas;
import com.etms.vo.PageResult;
import com.etms.vo.Result;
import com.etms.vo.UserParmas;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Result userLogin(UserParmas user) {
        if(user.getUsername()==null||user.getPassword()==null){
            return Result.failed();
        }
        SysUser u=userMapper.findByUnAndPwd(user);
        if(u==null){
            return Result.failed();
        }
        return Result.successed(u);
    }

    @Override
    public Result getList(PageParmas pageParmas) {
        PageHelper.startPage(pageParmas.getCurrentPage(), pageParmas.getPageSize());
        List<SysUser>list=userMapper.findAll();
        PageInfo pageInfo=new PageInfo(list);
        PageResult pageResult=new PageResult();
        pageResult.setList(list);
        pageResult.setTotalPage(pageInfo.getPages());
        return Result.successed(pageResult);
    }

    @Override
    public Result findById(Integer id) {
        SysUser user=userMapper.findById(id);
        if(user==null){
            return Result.failed();
        }
        return Result.successed(user);
    }

    @Override
    public Result delById(Integer id) {
        boolean f=userMapper.delById(id);
        if(!f){
            return Result.failed();
        }
        return Result.successed(f);
    }

    @Override
    public Result updateOrAdd(SysUser user) {
        if(user.getId()!=null&&user.getId()!=0){//走更新
            userMapper.update(user);
            return Result.successed("修改信息成功!",null);
        }
        userMapper.addUser(user);
        return Result.successed("新增用户成功!",null);
    }
}
